package com.woniu.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woniu.ComplainFeignClient;
import com.woniu.JwtTemplate;
import com.woniu.OrderFeignClient;
import com.woniu.Result;
import com.woniu.dto.ComplainDto;
import com.woniu.pojo.Message;
import com.woniu.pojo.User;
import com.woniu.mapper.UserMapper;
import com.woniu.pojo.dto.UserDto;
import com.woniu.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dong
 * @since 2022-11-16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private JwtTemplate jwtTemplate;

    @Autowired
    private ComplainFeignClient complainFeignClient;

    @Autowired
    private OrderFeignClient orderFeignClient;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public Result<String> login(UserDto userDto) {
        // 通过手机查询信息
        String tel = userDto.getTel();
        Integer type = userDto.getType();
        // lambda
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        // 条件查询
        queryWrapper.eq(User::getTel, tel);
        User loginUser = this.getOne(queryWrapper);

        /**
         * type: 用户类型
         * 1： 用户
         * 2： 门店
         * 3： 后台管理
         */
        // 对比两个获取的登陆密码
        if(type == 1 && BCrypt.checkpw(userDto.getPassword(), loginUser.getPassword())){
            // 创建一个token
            System.out.println("创建一个token = " + loginUser);
            Map<String, Object> map = new HashMap<>();
            map.put("userId", loginUser.getId());
            String token = jwtTemplate.createToken(map);
            String key = "CAR:USER_ID" + loginUser.getId();
            stringRedisTemplate.opsForValue().set(key, token, 20, TimeUnit.MINUTES);
            return Result.ok(token);
        }
        // 对比两个获取的登陆密码
        if(type == 2 && BCrypt.checkpw(userDto.getPassword(), loginUser.getPassword())){
            // 创建一个token
            System.out.println("创建一个token = " + loginUser);
            Map<String, Object> map = new HashMap<>();
            map.put("shopId", loginUser.getId());
            String token = jwtTemplate.createToken(map);
            String key = "CAR:SHOP_ID" + loginUser.getId();
            stringRedisTemplate.opsForValue().set(key, token, 20, TimeUnit.MINUTES);
            return Result.ok(token);
        }
        // 对比两个获取的登陆密码
        if(type == 3 && BCrypt.checkpw(userDto.getPassword(), loginUser.getPassword())){
            // 创建一个token
            System.out.println("创建一个token = " + loginUser);
            Map<String, Object> map = new HashMap<>();
            map.put("adminId", loginUser.getId());
            String token = jwtTemplate.createToken(map);
            String key = "CAR:ADMIN_ID" + loginUser.getId();
            stringRedisTemplate.opsForValue().set(key, token, 20, TimeUnit.MINUTES);
            return Result.ok(token);
        }

        return Result.ok();
    }

    /**
     * 提交投诉
     * @param complainDto 客户反馈的投诉对象
     * @return
     */
    @Override
    @Transactional
    public boolean submitComplainByOrderId(ComplainDto complainDto) {
        Boolean flag = complainFeignClient.submitComplainByDto(complainDto).getData();

        if(flag){
            Integer complainId = complainFeignClient.getByOrderId(complainDto.getOrderId()).getData().getId();
            Integer shopId = orderFeignClient.getShopId(complainDto.getOrderId()).getData();
            Message message = new Message();
            message.setType(1);
            message.setContent("你有一条投诉消息，请尽快处理");
            message.setStatus(0);
            message.setUserId(complainDto.getUserId());
            message.setShopId(shopId);
            rabbitTemplate.convertAndSend("complain-message-exchange","message",message);
            rabbitTemplate.convertAndSend("complain-const-exchange","message",complainId);
            return flag;
        }
        return false;
    }
}
